
import { conversationTemplate } from "../config.js"
import { readFileSync, writeFileSync } from "fs"
let conTemp: Conversation.IConversationTemplate = conversationTemplate
export namespace Conversation {
    // 对话模型类型
    // Creative：创造力的，Precise：精确的，Balanced：平衡的
    type ConversationStyle = 'Creative' | 'Precise' | 'Balanced'
    // 对话方式
    type ConversationType = 'SearchQuery' | 'Chat' // bing搜索，聊天
    // 模型映射
    export enum ConversationStr {
        Creative = 'h3imaginative',
        Precise = 'h3precise',
        Balanced = 'galileo'
    }
    // 发起对话时传入的参数
    export type IConversationOpts = {
        convStyle: ConversationStyle
        messageType: ConversationType
        conversationId: string
        conversationSignature: string
        clientId: string
    }
    type IMessage = {
        author: string,
        text: string,
        messageType: ConversationType,
    }
    type IArguments = {
        source: string
        optionsSets: string[]
        allowedMessageTypes: string[]
        isStartOfSession: boolean
        message: IMessage
        conversationId: string
        conversationSignature: string
        participant: {
            id: string
        }
    }
    // 发起对话的模板
    export type IConversationTemplate = {
        arguments: IArguments[]
        invocationId: string
        target: string
        type: number
    }
}
// 默认使用平衡类型
const { Balanced } = Conversation.ConversationStr
// 数据文件缓存(暂时没用上，调试的时候用的)
export function ctrlTemp(path?: string): any
export function ctrlTemp(path?: string, file?: any): void
export function ctrlTemp(path: string = "./temp", file?: string) {
    try {
        if (file) {
            return writeFileSync(path, file, "utf8")
        }
        return readFileSync(path, "utf8")
    } catch (error) { }
}

// 配置socket鉴权及消息模板
export function setConversationTemplate(params: Partial<Conversation.IConversationOpts> = {}): Conversation.IConversationTemplate {
    const { convStyle = Balanced, messageType = "Chat", conversationId,
        conversationSignature, clientId } = params
    if (!conversationId || !conversationSignature || !clientId) return null
    const args = conTemp.arguments[0]
    conTemp.arguments[0] = {
        ...args,
        conversationId,
        conversationSignature,
        participant: { id: clientId }
    }
    args.optionsSets.push(convStyle)// 这里传入对话风格
    args.message.messageType = messageType// 这里传入对话类型
    return conTemp
}